package service

import (
	"context"
	"gf_ruoyi/internal/consts"
	"gf_ruoyi/internal/model/entity"
)

type (
	sSession struct{}
)

var (
	insSession = sSession{}
)

//Session returns the interface of Session service
func Session() *sSession {
	return &insSession
}

//SetUser sets user into the session
func (s *sSession) SetUser(ctx context.Context, user *entity.User) error {
	return Context().Get(ctx).Session.Set(consts.UserSessionKey, user)
}

//GetUser retrieves and return the user from session
//It returns nil if the user did not sign in
func (s *sSession) GetUser(ctx context.Context) *entity.User {
	customCtx := Context().Get(ctx)
	if customCtx != nil {
		if v := customCtx.Session.MustGet(consts.UserSessionKey); !v.IsNil() {
			var user *entity.User
			_ = v.Struct(&user)
			return user
		}
	}
	return nil
}

//RemoveUser removes user rom session
func (s *sSession) RemoveUser(ctx context.Context) error {
	customCtx := Context().Get(ctx)
	if customCtx != nil {
		return customCtx.Session.Remove(consts.UserSessionKey)
	}
	return nil
}
